package classes.pack;

import park.pack.R;
import park.pack.ShowMap;
import classes.abstr.ExtInterface;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class UIMain extends Activity
{	
	
	
	public Button btnProfile;
	public Button btnStartService; 
    public Button btnStopService;
    public Button btnStartParking;
    public Button btnStopParking;
    public Button btnMap;
    public Button btnStatus;
    
    
    public TextView txtStatus;
        
	public void onCreate(Bundle savedInstanceState) 
    {
		super.onCreate(savedInstanceState);  
		
		Log.i("TEST", "Started MAIN");		
		StartupManager startupManager = new StartupManager(this);	//creates all classes, and loads all content
				
		//Intent myIntent = new Intent(this, TestTimer.class);
		//startService(myIntent);
		//create and show Gui layout
		setContentView(R.layout.main);
		
		btnStartService = (Button)findViewById(R.id.btnStartService);
        btnStopService = (Button)findViewById(R.id.btnStopService);
        btnStartParking = (Button)findViewById(R.id.btnStartParking);
        btnStopParking = (Button)findViewById(R.id.btnStopParking);
        txtStatus = (TextView)findViewById(R.id.txtStatus);
        btnStatus = (Button)findViewById(R.id.btnStatus);
		
        AppStatus status = Logging.getLatestStatus();
        
        switch(status)
        {
	        case NOT_PARKING:
	        	btnStartParking.setVisibility(View.VISIBLE);//Visible
	        	btnStopParking.setVisibility(View.GONE);//Gone
	        	break;
	        case PARKING:
				btnStartParking.setVisibility(View.GONE);
		        btnStopParking.setVisibility(View.VISIBLE);
		        break;
	        case FREE_PARKING:
				btnStartParking.setVisibility(View.GONE);
		        btnStopParking.setVisibility(View.VISIBLE);
		        break;		        
        }
    	
        //if(BackGroundService.isRunning())
        //{
    	//	btnStartService.setVisibility(View.GONE);
        //    btnStopService.setVisibility(View.VISIBLE);
        //}
        //else
        //{
    	//	btnStartService.setVisibility(View.VISIBLE);//Visible
        //    btnStopService.setVisibility(View.GONE);//Gone
        //}
        
        //txtStatus.setVisibility(View.VISIBLE);
        //btnStatus.setVisibility(View.VISIBLE);//Change to gone for noShow

		
		//set listeners
		SetOnClickListeners();    
		if(BackGroundService.isRunning())
		{
			btnStartService.setVisibility(View.GONE);//Gone
			btnStopService.setVisibility(View.VISIBLE);//Visible
		}
		else
		{
			btnStartService.setVisibility(View.VISIBLE);//Gone
			btnStopService.setVisibility(View.GONE);//Visible
		}
		
        
		//showStatusPopUp("TEST EK LKGLADF");//test message
		//showPopup(PopUpType.PARKING_STARTED);//testing pop up messages
    }	

	
	public void SetOnClickListeners()
	{
		btnStartService = (Button)findViewById(R.id.btnStartService);
        btnStopService = (Button)findViewById(R.id.btnStopService);
        btnStartParking = (Button)findViewById(R.id.btnStartParking);
        btnStopParking = (Button)findViewById(R.id.btnStopParking);
        btnMap			= (Button)findViewById(R.id.btnMaps);
        btnProfile		= (Button)findViewById(R.id.btnProfile);
        btnStatus		= (Button)findViewById(R.id.btnStatus);
        txtStatus		= (TextView)findViewById(R.id.txtStatus);
        
        btnStartParking.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{
        		Gui.startParkingSequence();
        	}
        });
        
        btnStatus.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{
        		AppStatus status = Logging.getLatestStatus();        		
        		txtStatus.setText(status.toString());
        	}
        });
        
        
        btnStopParking.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{     
        		Gui.stopParkingSequence();
        	}
        });
        
        btnStartService.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{          		
        		//gui.getLogging().testShowLogs();
        		Logging.testShowLogs();
        		//gui.getBackGroundService().startService();
        		
        		btnStartService.setVisibility(View.GONE);//Gone
                btnStopService.setVisibility(View.VISIBLE);//Visible
        		
        		Intent myIntent = new Intent(UIMain.this, BackGroundService.class);
        		startService(myIntent);
        	}
        });
        
        btnProfile.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{          	
        		  Gui.getProfiling().setView();
        	}
        });
        
        
        btnStopService.setOnClickListener(new View.OnClickListener() 
        {
        	
        	public void onClick(View v) 
        	{     
        		/*
        		if(gui.getParkingZones().inParkingZone(new GeoPoint(52637438,4851122)))//52642731,4725840//52637438,4851122
        		{
        			gui.showPopUp("TEST", "In parking zone");
        		}*/
        		//gui.getBackGroundService().stopService();
        		//Intent myIntent = new Intent(UIMain.this, BackGroundService.class);
        		

        		//stopService(bgService);
        		
        		btnStartService.setVisibility(View.VISIBLE);
                btnStopService.setVisibility(View.GONE);
                
                BackGroundService.stopService = true;
        		Intent myIntent = new Intent(UIMain.this, BackGroundService.class);
        		stopService(myIntent);

        	}
        });
        
        btnMap.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v) 
        	{      
        		Intent intent = new Intent(UIMain.this, ShowMap.class);
        		startActivity(intent);     
        	}
        });
	}
	
	
}
